package com.yunyike.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yunyike.constants.CartConstants;
import com.yunyike.constants.CommonConstant;
import com.yunyike.dto.SettlementDTO;
import com.yunyike.mapper.RecepientMapper;
import com.yunyike.model.IdentityCard;
import com.yunyike.model.Recepient;
import com.yunyike.redis.JedisManager;
import com.yunyike.service.IIdentityCardService;
import com.yunyike.service.IRecepientService;

/**
 * @author zhengqiang.shi
 * 2015年11月26日 下午10:17:59
 */
@Service("recepientServiceImpl")
public class RecepientServiceImpl implements IRecepientService {

	private static Logger log = LoggerFactory.getLogger(RecepientServiceImpl.class);
	
	@Autowired
	private RecepientMapper recepientMapper;
	@Autowired
	private IIdentityCardService idCardService;
	/**
	 * 保存收货地址
	 */
	public int addRecepient(Recepient recepient) {
		
		log.info("save recepient...,{}",recepient);
		
		int result;
		try {
			result = recepientMapper.insert(recepient);
		} catch (Exception e) {
			log.info("save recepient failed,message:"+e.getMessage(),e);
			return CommonConstant.NUMBER_0;
		}
		log.info("save recepient end,result:"+result);
		return result;
	}

	/**
	 * 更新收货地址
	 */
	public int updateRecepient(Recepient recepient) {
		
		log.info("update recepient...,{}",recepient);
		
		int result;
		try {
			result = recepientMapper.updateByPrimaryKeySelective(recepient);
		} catch (Exception e) {
			log.info("update recepient failed,message:"+e.getMessage(),e);
			return CommonConstant.NUMBER_0;
		}
		log.info("update recepient end,result:"+result);
		return result;
	}

	/**
	 * 获取用户的收货地址
	 */
	public List<Recepient> findRecepientByUser(String userId) {

		log.info("findRecepientByUser...userId:"+userId);
		
		List<Recepient> list = null;
		try {
			list = recepientMapper.findRecepientByUser(userId);
		} catch (Exception e) {
			log.error("findRecepientByUser failed.message:"+e.getMessage(),e);
			return null;
		}

		log.info("findRecepientByUser end.");
		return list;
	}

	/**
	 * 选择收货地址
	 */
	public int chooseRecepient(Recepient recepient, String sessionId) {
		
		log.info("choose recepient...");
		
		// settlement redis key
		log.info("----->> generator settlement key...");
		String settlementKey = CartConstants.settlementKey(sessionId);
		log.info("settlement key:"+settlementKey);
		
		// 获取当前结算信息
		log.info("get settlement data from redis...");
		try {
			SettlementDTO settlementDTO = (SettlementDTO)JedisManager.getObject(settlementKey);
			settlementDTO.setRecepient(recepient);
			if(recepient!=null){
				IdentityCard idcard= idCardService.getIdentityCardByName(recepient.getRecepientName());
				settlementDTO.setIdCard(idcard);
			}
			JedisManager.setObject(settlementKey, CartConstants.SETTLEMENT_OUT_TIME, settlementDTO);
		} catch (Exception e) {
			log.error("chooseRecepient has error,message:"+e.getMessage(),e);
			return CommonConstant.NUMBER_0;
		}
		
		log.info("choose recepient end.");
		return CommonConstant.NUMBER_1;
	}

	@Transactional
	public int setDefaultRecepient(Recepient recepient) {
		// 更新所有收货地址为 非默认收货地址
		recepientMapper.updateDefault();
		
		// 更新当前收货地址为默认地址
		return recepientMapper.updateByPrimaryKeySelective(recepient);
	}
	
	public boolean checkCurrentRecepient(Recepient recepientDB, String sessionId){
		log.info("checkCurrentRecepient...");
		
		// settlement redis key
		log.info("----->> generator settlement key...");
		String settlementKey = CartConstants.settlementKey(sessionId);
		log.info("settlement key:"+settlementKey);
		
		try {
			SettlementDTO settlementDTO = (SettlementDTO)JedisManager.getObject(settlementKey);
			Recepient recepient = settlementDTO.getRecepient();
			if(recepient.getRecepientId().equals(recepientDB.getRecepientId())){
				log.info("checkCurrentRecepient...this recepient is the currently selected...");
				return true;
			}
		
		} catch (Exception e) {
			log.error("checkCurrentRecepient has error,message:"+e.getMessage(),e);
		}
		
		return false;
	}

	/**
	 * 获取收货地址
	 */
	public Recepient findByIdAndUserId(String recepientId, String userId) {
		
		Recepient recepient;
		try {
			recepient = recepientMapper.selectByPrimaryKey(recepientId);
		} catch (Exception e) {
			log.error("recepientMapper.selectByPrimaryKey has error.message:"+e.getMessage(),e);
			return null;
		}
		if(recepient == null){
			log.info("not found recepient by {}",recepientId);
			return null;
		}
		
		String curUserId = recepient.getUserId();
		if(!userId.equals(curUserId)){
			log.error("recepient not for userId:{},curUserId:{}",userId,curUserId);
			return null;
		}
		
		return recepient;
	}
	
}
